package org.kth.dks.dks_node;

import junit.framework.TestCase;

import org.kth.dks.dks_comm.ConnectionManager;
import org.kth.dks.dks_comm.DKSRef;
import org.kth.dks.util.MathMiscConstant;
import org.kth.dks.util.Pair;

public class DKSNodeTest extends TestCase {

	DKSNode dksNode = null;
	ConnectionManager cm = null;
	
	public void testCalculateIntervals() throws Exception {
		dksNode.N = 64;
		dksNode.K = 4;
		dksNode.L = 3;
		dksNode.math = new MathMiscConstant(dksNode.N, dksNode.K);
		
		dksNode.setConnectionManager(cm, DKSRef.newDKSRef("dks://localhost:4440/0/63/0"));
		
		Pair r = dksNode.getNodeIntervals(0);
		int l = ((Integer) r.first()).intValue();
		int k = ((Integer) r.second()).intValue();
		assertEquals(l, 3);
		assertEquals(k, 1);
		
		r = dksNode.getNodeIntervals(62);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 1);
		assertEquals(k, 3);
		
		r = dksNode.getNodeIntervals(15);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 1);
		assertEquals(k, 1);

		r = dksNode.getNodeIntervals(2);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 3);
		assertEquals(k, 3);

		r = dksNode.getNodeIntervals(3);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 2);
		assertEquals(k, 1);

		dksNode.N = 256;
		dksNode.K = 2;
		dksNode.L = 8;
		dksNode.math = new MathMiscConstant(dksNode.N, dksNode.K);
		dksNode.myDKSRef = DKSRef.newDKSRef("dks://localhost:0/0/0/0");
		
		r = dksNode.getNodeIntervals(1);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 8);
		assertEquals(k, 1);

		r = dksNode.getNodeIntervals(127);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 2);
		assertEquals(k, 1);

		r = dksNode.getNodeIntervals(128);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(l, 1);
		assertEquals(k, 1);

		r = dksNode.getNodeIntervals(0);
		l = ((Integer) r.first()).intValue();
		k = ((Integer) r.second()).intValue();
		assertEquals(k, 0);
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		dksNode = new DKSNode();
		cm = ConnectionManager.getInstance(4440);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		cm.end();
	}

}
